<?php
/**
 * Administra los articulos del sitio
  *@author Luis Gabriel Rodriguez <luisgabriel84@gmail.com>
 */

/**
 * Administra los articulos del sitio
 */
class ArticulosController extends AppController {

	var $name = 'Articulos';
        /**
        * @var array
        * @access public
        */
	var $helpers = array('Html', 'Form', 'Javascript','Fck');
        /**
        * @var array
        * @access public
        */
        var $components =array('Filehandler');
        function beforeFilter()
        {

            $this->Auth->allow('index','view');
        }
        /**
         *
         * Muestra los archivos en orden descendente
         */
	function index() {
		$this->Articulo->recursive = 0;
                if(isset($this->params['requested']))
                {
                    $articulos = $this->Articulo->find('all',array('order'=>'Articulo.created DESC','limit'=>10));
                    return $articulos;
                }
		$this->set('articulos', $this->paginate());
	}

        /**
         *
         * Muestra el articulo
         * @param string $slug Identificador del articulo
         */
	function view($slug = null) {
		if (!$slug) {
			$this->Session->setFlash(__('Invalid Articulo.', true));
			$this->redirect(array('action'=>'index'));
		}
                $id = $this->Articulo->findBySlug($slug);

		$this->set('articulo', $this->Articulo->read(null, $id['Articulo']['id']));
	}



        /**
         *
         * Listado de articulos para el administrador
         */

	function admin_index() 
        {
                            if(!$this->Acces->check_resctriction(1))
            {
                $this->redirect('/promociones');
            }
                $this->Acces->check_resctriction( array(1 ));
                $this->layout = 'admin';
		$this->Articulo->recursive = 0;
		$this->set('articulos', $this->paginate());
	}


         /**
         *
         * Agregar un nuevo articulo
         *
         */
	function admin_add() {
                        if(!$this->Acces->check_resctriction(1))
            {
                $this->redirect('/promociones');
            }
                $this->layout = 'admin';
		if (!empty($this->data)) {
                        $this->data['Articulo']['slug'] = $this->slug($this->data['Articulo']['titulo']);
			$this->Articulo->create();

                        if(!empty($this->data['Articulo']['file']['name']))
                        {
                            $this->data['Articulo']['file']=$this->Filehandler->uploadpicture('img/articulos/',$this->data['Articulo']['file'],350,120);
                        }
                        else
                        {
                            $this->data['Product']['file'] ='';
                        }

			if ($this->Articulo->save($this->data)) {
				$this->Session->setFlash(__('The Articulo has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Articulo could not be saved. Please, try again.', true));
			}
		}
	}
         /**
         *
         * Modificar el articulo
         * @param integer $id Identificador del articulo
         */
	function admin_edit($id = null) {
            if(!$this->Acces->check_resctriction(1))
            {
                $this->redirect('/promociones');
            }
                $this->layout = 'admin';
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Articulo', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
                    $file =  $this->Articulo->read('file', $id);
                    if($this->data['Articulo']['file']['name']!="" )
		    {
                            if( $file['Articulo']['file']!='' )
                            {
                                $this->Filehandler->deleteFile('img/articulos/',$this->data['Articulo']['file'],true);
                            }
                            //Subir y crear Imagenes
                            $this->data['Articulo']['file']=$this->Filehandler->uploadpicture('img/articulos/',$this->data['Articulo']['file'],350,120);
                     }
		     else
                     {
                          $this->data['Articulo']['file'] = $file['Articulo']['file'];
                     }

                        $this->data['Articulo']['slug'] = $this->slug($this->data['Articulo']['titulo']);
			if ($this->Articulo->save($this->data)) {
				$this->Session->setFlash(__('The Articulo has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Articulo could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Articulo->read(null, $id);
		}
	}
        
        /**
         *
         * Eliminar el articulo
         * @param integer $id Identificador del articulo
         */
	function admin_delete($id = null) 
        {                           if(!$this->Acces->check_resctriction(1))
            {
                $this->redirect('/promociones');
            }
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Articulo', true));
			$this->redirect(array('action'=>'index'));
		}
                
                $file =  $this->Product->read('file', $id);
                $this->Filehandler->deleteFile('img/articulos/',$file['Product']['file'],true);
		if ($this->Articulo->del($id)) {
			$this->Session->setFlash(__('Articulo deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>